<?php

namespace apiend\modules\user\models;

use Yii;
use yii\data\ActiveDataProvider;
use common\helpers\Url;

class FollowSchoolSearch extends \common\models\school\School {

    /**
     * 关注列表
     * @param array $params
     * @return \yii\data\ActiveDataProvider
     */
    public function search($uid) {
        $query = static::find()
        ->where([
            'status' => static::STATUS_ACTIVE,
        ]);
        $query->joinWith('follow f')->andWhere(['f.user_id' => $uid]);
        $query->orderBy([
            'f.listorder' => SORT_ASC,
            'f.created_at' => SORT_DESC,
        ]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id',
            'name',
            'grade',
            'cover' => function () {
                return Url::toRemote($this->cover);
            },
            'region',
            'address_detail',
            'link' => function () {
                return Url::h5('/school/' . $this->id);
            },
        ];
    }

}
